<template>
  <div class="login-page">
    <div class="login-box">
      <h1>稀土元素系统</h1>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item :label="'密\xa0\xa0\xa0码'" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <div class="code-box">
            <el-input v-model.number="ruleForm.code"></el-input>
            <img height=45 :src="img" @click="getCode()"/>
          </div>
        </el-form-item>
        <el-form-item id="login-btn-box">
          <el-button class="login-btn" type="primary" @click="submitForm('ruleForm')">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import {usernameValidator,passwordValidator,codeValidator} from '@/utils/Validator'
import {getCode,login} from "@/request/Api";
import {mapActions} from "vuex";
import {params} from "@/enums/PublicEnums";
export default {
  name: 'HomeViews',
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: 'admin123',
        code: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: usernameValidator, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: passwordValidator, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger:'blur'},
          { validator: codeValidator, trigger: 'blur' }
        ]
      },
      img: ''
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(
          async (valid) => {
        if (valid) {
          let res=await login({
            username: this.ruleForm.username,
            password: this.ruleForm.password,
            code: this.ruleForm.code,
            uuid: localStorage.getItem(params.uuid),
          })
          if(!res){return;}
          localStorage.setItem(params.token,res.token);//保存token
          localStorage.removeItem(params.uuid);//清除验证码的uuid
          this.$router.push('/').catch(err=>{
            console.log(err)
          })//跳转首页
          //发送请求 获取用户信息
          this.userInfoDataRequest()
          this.$message.success('登陆成功');
        } else {
          this.$message.error("参数格式不对");
          return false;
        }
      });
    },
    //发送请求 获取用户信息
    ...mapActions(params.userInfoData,[params.userInfoDataRequest]),
    async getCode(){
      let res=await getCode();
      if(!res){return;}
      this.img="data:image/gif;base64,"+res.img
      //浏览器cookies存储
      localStorage.setItem(params.uuid,res.uuid)
    },
  },
  created() {
    this.getCode();
  }
}
</script>


<style scoped>
  .login-page {
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-size: cover; /* 确保背景图覆盖整个元素 */
    background-position: center; /* 确保背景图居中显示 */
    background-image: url("@/assets/bg.gif");
    position: relative;/**相对**/
  }
  .login-box{
    width: 350px;
    position: absolute;/**绝对**/
    top: 35%;
    left: 35%;
    background-color: aliceblue;
    border-radius: 20px;
    padding-top: 20px;
    padding-right: 45px;
  }
  h1{
    text-align: center;
    margin-bottom: 10px;
    margin-left: 30px;
  }
  .code-box{
    display: flex;
  }
  img{
    margin-left: 20px;
  }
  .login-btn{
    width: 100%;
  }
  #login-btn-box{
    margin-left: -60px;
  }

</style>
